package com.otomotion.research.ants.databank;

import com.otomotion.research.ants.information.Coordinate;
import com.otomotion.research.ants.util.LOGGER;
import java.util.BitSet;

/**
 * @author    joel
 */
public class DirtRepository 
{
	int sizeX;
	int sizeY;
	int sizeZ;
    DirtLayer[] layer;
    
    public DirtRepository(int x, int y, int z)
    {
    	this.sizeX=x;
    	this.sizeY=y;
    	this.sizeZ=z;
    	
        LOGGER.logln("Creating DirtRepository dimensioned at: "+new Coordinate(x,y,z).toString()+" ... ");

        layer = new DirtLayer[z];
        for(int i=0;i<z;i++)
        {
            layer[i]= new DirtLayer(x,y);
            if (i<z/2)
                layer[i].dirtify();
            if (i==z-1)
                layer[i].dirtify();
            
        }
        //make walls
        for (int k=z/2;k<z;k++)
        {
            for (int i =0; i<x;i++)
            {
                this.setDirt(i, 0, k);
                this.setDirt(i, y-1, k);
            }
            for (int j =0; j<y;j++)
            {
                this.setDirt(0, j, k);
                this.setDirt(x-1, j, k);
            }
        }
        LOGGER.log("DONE");
    }

    /* (non-Javadoc)
     * @see com.otomotion.research.ants.databank.DirtRepositoryI#isDirt(int, int, int)
     */
    public boolean isDirt(int x, int y, int z)
    {
    	if (this.isInBounds(new Coordinate(x,y,z)))
    	{
    		return layer[z].isDirt(x, y);	
    	}
    	else return false;
        
    }


    private boolean isInBounds(Coordinate c) {

    	if (c.getX()<0||c.getX()>sizeX-1
    			|| c.getY()<0||c.getY()>sizeY-1
    			|| c.getZ()<0||c.getZ()>sizeZ-1)
    	{
    		return false;
    	}
    	return true;
		
	}

	/* (non-Javadoc)
     * @see com.otomotion.research.ants.databank.DirtRepositoryI#setDirt(int, int, int)
     */
	public boolean setDirt(int x, int y, int z)
    {
        return layer[z].setDirt(x, y);
    }

    /* (non-Javadoc)
     * @see com.otomotion.research.ants.databank.DirtRepositoryI#clearDirt(int, int, int)
     */
    public boolean clearDirt(int x, int y, int z)
    {
        return layer[z].clearDirt(x, y);
    }
}
